using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.IO;

using GonzalesCore.CoreElements;
using GonzalesCore.CoreManagers;

using Jamila2.CoreElements;
using Jamila2.Database;

namespace BPCSFileDaemon.FileFactory
{
    public abstract class FileFactoryBase : ManagerBase, IDisposable
    {
        protected string path = "";
        protected ArrayList FileLines = new ArrayList();
        protected SQLFactory CurSQLFactory = null;
        protected DatabaseEngine CurDBEngine = null;
        protected string DBConnectionString = "";
        protected TypeOfDatabase DatabaseType;
        protected bool is_ready = false;
        protected string EntityID = "";
        protected PersonalSessionAccount CurrentSystemSession = null;

        public bool IsReady
        {
            get
            {
                return this.is_ready;
            }
        }

        public FileFactoryBase(string path, string DBConnectionString, TypeOfDatabase DatabaseType)
        {
            this.path = path;
            this.DBConnectionString = DBConnectionString;
            this.DatabaseType = DatabaseType;

            this.CurSQLFactory = new SQLFactory(this.DatabaseType);
            this.CurrentSystemSession = new PersonalSessionAccount(null, this.DatabaseType, this.DBConnectionString);
        }

        public FileFactoryBase(string path, string EntityID, string DBConnectionString, TypeOfDatabase DatabaseType)
        {
            this.path = path;
            this.DBConnectionString = DBConnectionString;
            this.DatabaseType = DatabaseType;
            this.EntityID = EntityID;

            this.CurSQLFactory = new SQLFactory(this.DatabaseType);
            this.CurrentSystemSession = new PersonalSessionAccount(null, this.DatabaseType, this.DBConnectionString);
        }

        public virtual bool ReadFile()
        {
            bool smooth = true;

            try
            {
                using (StreamReader Reader = new StreamReader(this.path, UTF8Encoding.UTF8))
                {
                    FileLines.Clear();

                    string line;
                    // Read and display lines from the file until the end of 
                    // the file is reached.
                    while ((line = Reader.ReadLine()) != null)
                    {
                        FileLines.Add(line);
                    }

                    Reader.Close();
                }
            }
            catch (Exception ex)
            {
                smooth = false;
                this.ErrMsg = "[FileFactoryBase.ReadFile] : Exception : " + ex.Message;
            }


            return smooth;
        }

        #region TryConnection
        protected virtual bool TryConnection()
        {
            try
            {
                if (!this.is_ready)
                {
                    this.CurDBEngine = new DatabaseEngine(this.DatabaseType, this.DBConnectionString);

                    if (this.CurDBEngine.Connect())
                    {
                        this.is_ready = true;
                    }
                    else
                    {
                        this.ErrMsg = this.CurDBEngine.ErrorMessage;
                    }
                }

            }
            catch (Exception ex)
            {
                this.ErrMsg = ex.Message;
            }

            return this.is_ready;
        }
        #endregion

        public override void Dispose()
        {
            if (this.IsReady)
            {
                this.CurDBEngine.Disconnect();
            }

            this.CurDBEngine = null;
            this.CurSQLFactory = null;

            base.Dispose();
        }

    }

}
